package service;

import java.util.ArrayList;
import java.util.List;


import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;

import dao.BranchesMapper;
import entity.Branches;

public class BranchesService {
	public List<Branches> queryAllBranches() {
		SqlSession sqlSession = null;
		List<Branches> list = new ArrayList<Branches>();
		try {

			sqlSession = MybatisUtil.createSqlSession();

			list = sqlSession.getMapper(BranchesMapper.class).queryAllBranches();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MybatisUtil.closeSqlSession(sqlSession);
		}
		return list;
	}

	/**
	 * ���id��ѯָ�������Ϣ
	 *
	 * @param id �����
	 * @return ָ��������
	 */
	public Branches queryBranchesById(int id) {
		SqlSession sqlSession = null;
		Branches branches = new Branches();
		try {

			sqlSession = MybatisUtil.createSqlSession();

			branches = sqlSession.getMapper(BranchesMapper.class).queryBranchesById(id);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MybatisUtil.closeSqlSession(sqlSession);
		}
		return branches;
	}

	/**
	 * ���Ӫҵִ�պŲ�ѯָ�������Ϣ
	 *
	 * @param businessNo Ӫҵִ�պ�
	 * @return �����Ϣ����
	 */
	public Branches queryBranchesByBusNo(String businessNo) {
		SqlSession sqlSession = null;
		Branches branches = new Branches();
		try {

			sqlSession = MybatisUtil.createSqlSession();

			branches = sqlSession.getMapper(BranchesMapper.class).queryBranchesByBusNo(businessNo);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MybatisUtil.closeSqlSession(sqlSession);
		}
		return branches;
	}

	/**
	 * ����µ������Ϣ�ķ�����
	 *
	 * @param branch �����������
	 * @return ��Ӱ������
	 */
	public int addBranch(Branches branch) {
		SqlSession sqlSession = null;
		int count = 0;
		try {
			sqlSession = MybatisUtil.createSqlSession();


			count = sqlSession.getMapper(BranchesMapper.class).addBranch(branch);

			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			MybatisUtil.closeSqlSession(sqlSession);
		}
		return count;
	}
}
